import unittest
import time
import random
from database.crud import add_event, get_events
from utils.time_utils import calculate_reminder_time

class PerformanceTest(unittest.TestCase):
    def test_database_performance(self):
        """测试数据库操作性能（1000条记录）"""
        # 清空数据库
        get_events()  # 确保获取所有事件
        
        # 添加1000条测试数据
        start_time = time.time()
        for i in range(1000):
            add_event(
                title=f"Performance Test {i}",
                description="Performance test data",
                event_time=calculate_reminder_time("2025-11-30 10:00", i),
                category="work"
            )
        add_time = time.time() - start_time
        
        # 测试查询性能
        start_time = time.time()
        events = get_events()
        query_time = time.time() - start_time
        
        # 验证性能
        self.assertLess(add_time, 0.5, "Adding 1000 events took too long")
        self.assertLess(query_time, 0.1, "Querying 1000 events took too long")
        
        print(f"Database performance test: Added 1000 events in {add_time:.4f}s, Query in {query_time:.4f}s")

    def test_time_calculation_performance(self):
        """测试时间计算性能（10000次计算）"""
        start_time = time.time()
        for _ in range(10000):
            calculate_reminder_time("2025-11-30 10:00", 30)
        calc_time = time.time() - start_time
        
        self.assertLess(calc_time, 0.1, "Time calculation is too slow")
        print(f"Time calculation performance: 10000 calculations in {calc_time:.4f}s")

if __name__ == '__main__':
    unittest.main(verbosity=1)